package com.huawei.appmarket.support.widget.tabhost;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
import o.Logger;
import o.ewj;
import o.ewk;
import o.FragmentManager;
import o.FragmentTransaction;

public class FragmentTabHost extends TabHost implements OnTabChangeListener, ewj {
    private c ʻ;
    private boolean ʼ;
    private ewk ʽ;
    private FrameLayout ˊ;
    private FragmentManager ˋ;
    private final ArrayList<c> ˎ = new ArrayList();
    private Context ˏ;
    private boolean ˏॱ;
    private int ॱ;
    private OnTabChangeListener ॱॱ;
    private d ᐝ;

    public interface d {
        void onTabSelected(int i);
    }

    static class SavedState extends BaseSavedState {
        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
            public /* synthetic */ Object createFromParcel(Parcel parcel) {
                return ˋ(parcel);
            }

            public /* synthetic */ Object[] newArray(int i) {
                return ˎ(i);
            }

            public SavedState ˋ(Parcel parcel) {
                return new SavedState(parcel);
            }

            public SavedState[] ˎ(int i) {
                return new SavedState[i];
            }
        };
        String ˏ;

        SavedState(Parcelable parcelable) {
            super(parcelable);
        }

        SavedState(Parcel parcel) {
            super(parcel);
            this.ˏ = parcel.readString();
        }

        public void writeToParcel(Parcel parcel, int i) {
            super.writeToParcel(parcel, i);
            parcel.writeString(this.ˏ);
        }

        public String toString() {
            return "FragmentTabHost.SavedState{" + Integer.toHexString(System.identityHashCode(this)) + " curTab=" + this.ˏ + "}";
        }
    }

    static class a implements TabContentFactory {
        private final Context ˊ;

        public a(Context context) {
            this.ˊ = context;
        }

        public View createTabContent(String str) {
            View view = new View(this.ˊ);
            view.setMinimumWidth(0);
            view.setMinimumHeight(0);
            return view;
        }
    }

    static final class c {
        Fragment ˊ;
        int ˋ;
        @NonNull
        String ॱ;

        c(int i, @NonNull String str) {
            this.ˋ = i;
            this.ॱ = str;
        }
    }

    public FragmentTabHost(Context context) {
        super(context, null);
        ˊ(context, null);
    }

    public FragmentTabHost(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        ˊ(context, attributeSet);
    }

    private void ˊ(Context context, AttributeSet attributeSet) {
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, new int[]{16842995}, 0, 0);
        this.ॱ = obtainStyledAttributes.getResourceId(0, 0);
        obtainStyledAttributes.recycle();
        super.setOnTabChangedListener(this);
    }

    private void ˏ(Context context) {
        if (findViewById(16908307) == null) {
            View linearLayout = new LinearLayout(context);
            linearLayout.setOrientation(1);
            addView(linearLayout, new LayoutParams(-1, -1));
            View tabWidget = new TabWidget(context);
            tabWidget.setId(16908307);
            tabWidget.setOrientation(0);
            linearLayout.addView(tabWidget, new LinearLayout.LayoutParams(-1, -2, 0.0f));
            tabWidget = new FrameLayout(context);
            tabWidget.setId(16908305);
            linearLayout.addView(tabWidget, new LinearLayout.LayoutParams(0, 0, 0.0f));
            tabWidget = new FrameLayout(context);
            this.ˊ = tabWidget;
            this.ˊ.setId(this.ॱ);
            linearLayout.addView(tabWidget, new LinearLayout.LayoutParams(-1, 0, 1.0f));
        }
    }

    @Deprecated
    public void setup() {
        throw new IllegalStateException("Must call setup() that takes a Context and FragmentManager");
    }

    public void setup(Context context, FragmentManager juVar) {
        ˏ(context);
        super.setup();
        this.ˏ = context;
        this.ˋ = juVar;
        ˋ();
    }

    public void setup(Context context, FragmentManager juVar, int i) {
        ˏ(context);
        super.setup();
        this.ˏ = context;
        this.ˋ = juVar;
        this.ॱ = i;
        ˋ();
        this.ˊ.setId(i);
        if (getId() == -1) {
            setId(16908306);
        }
    }

    private void ˋ() {
        if (this.ˊ == null) {
            this.ˊ = (FrameLayout) findViewById(this.ॱ);
            if (this.ˊ == null) {
                throw new IllegalStateException("No tab content FrameLayout found for id " + this.ॱ);
            }
        }
    }

    public void setOnTabSelectedListener(d dVar) {
        this.ᐝ = dVar;
    }

    public void setOnTabChangedListener(OnTabChangeListener onTabChangeListener) {
        this.ॱॱ = onTabChangeListener;
    }

    private void ˊ(@NonNull TabSpec tabSpec, int i) {
        tabSpec.setContent(new a(this.ˏ));
        String tag = tabSpec.getTag();
        c cVar = new c(i, tag);
        if (this.ʼ) {
            cVar.ˊ = this.ˋ.findFragmentByTag(tag);
            if (!(cVar.ˊ == null || cVar.ˊ.ʼॱ())) {
                FragmentTransaction ˊ = this.ˋ.ˊ();
                if (this.ˏॱ) {
                    ˊ.ॱ(cVar.ˊ);
                } else {
                    ˊ.ˋ(cVar.ˊ);
                }
                ˊ.ˋ();
            }
        }
        this.ˎ.add(cVar);
        addTab(tabSpec);
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        String currentTabTag = getCurrentTabTag();
        FragmentTransaction kaVar = null;
        int size = this.ˎ.size();
        for (int i = 0; i < size; i++) {
            c cVar = (c) this.ˎ.get(i);
            cVar.ˊ = this.ˋ.findFragmentByTag(cVar.ॱ);
            if (!(cVar.ˊ == null || cVar.ˊ.ʼॱ())) {
                if (cVar.ॱ.equals(currentTabTag)) {
                    this.ʻ = cVar;
                } else {
                    if (kaVar == null) {
                        kaVar = this.ˋ.ˊ();
                    }
                    if (this.ˏॱ) {
                        kaVar.ॱ(cVar.ˊ);
                    } else {
                        kaVar.ˋ(cVar.ˊ);
                    }
                }
            }
        }
        this.ʼ = true;
        FragmentTransaction ˊ = ˊ(currentTabTag, kaVar);
        if (ˊ != null) {
            ˊ.ˋ();
            this.ˋ.ॱ();
        }
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.ʼ = false;
    }

    protected Parcelable onSaveInstanceState() {
        Parcelable savedState = new SavedState(super.onSaveInstanceState());
        savedState.ˏ = getCurrentTabTag();
        return savedState;
    }

    protected void onRestoreInstanceState(Parcelable parcelable) {
        if (parcelable instanceof SavedState) {
            SavedState savedState = (SavedState) parcelable;
            super.onRestoreInstanceState(savedState.getSuperState());
            setCurrentTabByTag(savedState.ˏ);
            return;
        }
        super.onRestoreInstanceState(parcelable);
    }

    public void onTabChanged(String str) {
        if (this.ʼ) {
            FragmentTransaction ˊ = ˊ(str, null);
            if (ˊ != null) {
                ˊ.ˋ();
            }
        }
        if (this.ᐝ != null) {
            this.ᐝ.onTabSelected(getCurrentTab());
        }
        if (this.ॱॱ != null) {
            this.ॱॱ.onTabChanged(str);
        }
    }

    @Nullable
    private FragmentTransaction ˊ(@Nullable String str, @Nullable FragmentTransaction kaVar) {
        Fragment fragment = null;
        if (this.ʽ == null) {
            Logger.ˋ("FragmentTabHost", "doTabChanged, hostAdapter == null");
            return null;
        }
        if (kaVar == null) {
            kaVar = this.ˋ.ˊ();
        }
        c ˋ = ˋ(str);
        if (this.ʻ != ˋ) {
            ˎ(ˋ, kaVar);
        } else if (ˋ != null) {
            if (this.ˏॱ) {
                kaVar.ˏ(ˋ.ˊ);
            } else {
                kaVar.ˎ(ˋ.ˊ);
            }
            ewk o_ewk = this.ʽ;
            int i = ˋ.ˋ;
            Fragment fragment2 = ˋ.ˊ;
            if (this.ʻ != null) {
                fragment = this.ʻ.ˊ;
            }
            o_ewk.ˏ(i, fragment2, fragment);
        }
        return kaVar;
    }

    private void ˎ(c cVar, FragmentTransaction kaVar) {
        if (!(this.ʻ == null || this.ʻ.ˊ == null)) {
            if (this.ˏॱ) {
                kaVar.ॱ(this.ʻ.ˊ);
            } else {
                kaVar.ˋ(this.ʻ.ˊ);
            }
        }
        if (cVar != null) {
            if (cVar.ˊ == null) {
                cVar.ˊ = this.ʽ.ˎ(cVar.ˋ);
                kaVar.ॱ(this.ॱ, cVar.ˊ, cVar.ॱ);
            } else if (this.ˏॱ) {
                kaVar.ˏ(cVar.ˊ);
            } else {
                kaVar.ˎ(cVar.ˊ);
            }
            this.ʽ.ˏ(cVar.ˋ, cVar.ˊ, this.ʻ == null ? null : this.ʻ.ˊ);
        }
        this.ʻ = cVar;
    }

    public void setAdapter(ewk o_ewk) {
        if (this.ʽ == null) {
            this.ʽ = o_ewk;
            if (this.ʽ != null) {
                this.ʽ.ॱ(this);
                int ˎ = this.ʽ.ˎ();
                for (int i = 0; i < ˎ; i++) {
                    TabSpec newTabSpec = newTabSpec(this.ʽ.ˊ(i));
                    this.ʽ.ˎ(i, newTabSpec);
                    ˊ(newTabSpec, i);
                    getTabWidget().setDividerDrawable(null);
                }
            }
        }
    }

    public Fragment ॱ() {
        if (this.ʻ != null) {
            return this.ʻ.ˊ;
        }
        return null;
    }

    public void setForbidTabDestroyed(boolean z) {
        this.ˏॱ = z;
    }

    @Nullable
    private c ˋ(String str) {
        int size = this.ˎ.size();
        for (int i = 0; i < size; i++) {
            c cVar = (c) this.ˎ.get(i);
            if (cVar.ॱ.equals(str)) {
                return cVar;
            }
        }
        return null;
    }

    public void ˏ() {
        if (this.ʽ != null) {
            ˊ();
            int ˎ = this.ʽ.ˎ();
            for (int i = 0; i < ˎ; i++) {
                TabSpec newTabSpec = newTabSpec(this.ʽ.ˊ(i));
                this.ʽ.ˎ(i, newTabSpec);
                ˊ(newTabSpec, i);
                getTabWidget().setDividerDrawable(null);
            }
        }
    }

    private void ˊ() {
        if (this.ʼ) {
            FragmentTransaction ˊ = this.ˋ.ˊ();
            for (Fragment fragment : this.ˋ.ˏ()) {
                if (!(fragment == null || fragment.ʼॱ())) {
                    ˊ.ˋ(fragment);
                }
            }
            ˊ.ˋ();
            this.ˎ.clear();
            clearAllTabs();
            this.ˋ.ˏ().clear();
        }
    }
}
